﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Controls;

namespace ComUIControl
{
    public partial class IntRangeTrackBar : MetroUserControl, INotifyPropertyChanged
    {
        private string _fieldName = "name";
        public string RangeName
        {
            get
            {
                return _fieldName;
            }
            set
            {
                metroLabel1.Text = value;
                _fieldName = value;
            }
        }

        public IntRangeTrackBar()
        {
            InitializeComponent();
            metroTextBox1.DataBindings.Add("Text", this, "Value");
            metroTrackBar1.Minimum = (int)MinValue;
            metroTrackBar1.Maximum = (int)MaxValue;
        }

        private int _value = 50;
        [DefaultValue(50)]
        public int Value
        {
            get
            {
                return _value;
            }
            set
            {
                if (value != _value)
                {
                    RaisePropertyChanged("Value");
                }
                _value = value;
                if (value <= MaxValue && value >= MinValue)
                {
                    metroTrackBar1.Value = value;
                    metroTextBox1.Text = value.ToString();
                }
            }
        }

        private int _minValue = 0;
        [DefaultValue(0)]
        public int MinValue
        {
            get
            {
                return _minValue;
            }
            set
            {
                _minValue = value;
                if (metroTrackBar1 != null)
                {
                    metroTrackBar1.Minimum = (int)value;
                }
            }
        }

        private int _maxValue = 100;
        [DefaultValue(100)]
        public int MaxValue
        {
            get
            {
                return _maxValue;
            }
            set
            {
                _maxValue = value;
                if (metroTrackBar1 != null)
                {
                    metroTrackBar1.Maximum = value;
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public delegate void UpdatePropertyDelegate(string strName);
        private void RaisePropertyChanged(string propertyName)
        {
            if (this.InvokeRequired)
            {
                UpdatePropertyDelegate outdelegate = new UpdatePropertyDelegate(RaisePropertyChanged);
                this.BeginInvoke(outdelegate, new object[] { propertyName });
                return;
            }
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void metroTrackBar1_Scroll(object sender, ScrollEventArgs e)
        {
            Value = metroTrackBar1.Value;
        }

        private void metroTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b'
                 || Char.IsDigit(e.KeyChar)
                 || Char.IsControl(e.KeyChar)
                 || e.KeyChar == 46)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void metroTextBox1_Validating(object sender, CancelEventArgs e)
        {
            int val;
            if (int.TryParse(metroTextBox1.Text, out val))
            {
                if (val < MinValue || val > MaxValue)
                {
                    string infoErr = string.Format("请输入一个[{0},{1}]范围内的整数.", MinValue, MaxValue);
                    errorProvider1.SetError(metroTextBox1, infoErr);
                }
                else
                {
                    //Value = val;
                    errorProvider1.SetError(metroTextBox1, "");
                }
            }
            else
            {
                errorProvider1.SetError(metroTextBox1, "不是合法的数字!");
                metroTextBox1.Focus();
            }
        }
    }
}
